//
//  NSDate+LZDate.m
//  LZBaseFrame
//
//  Created by 位立志 on 2018/3/12.
//  Copyright © 2018年 Bwton. All rights reserved.
//

#import "NSDate+SJZCategory.h"
#import <UIKit/UIKit.h>

static NSDateFormatter *_dateFormatter = nil;

@implementation NSDate (SJZCategory)

- (NSDateFormatter*)getDateFormatter{
    if (_dateFormatter == nil) {
        _dateFormatter = [[NSDateFormatter alloc]init];
    }
    return _dateFormatter;
}

+ (NSDateFormatter*)getDateFormatter{
    if (_dateFormatter == nil) {
        _dateFormatter = [[NSDateFormatter alloc]init];
    }
    return _dateFormatter;
}

- (NSString *)getTimeStamp{
    NSString *timeStamp = [NSString stringWithFormat:@"%ld",(long)[self timeIntervalSince1970]];
    return timeStamp;
}

+ (NSDate *)getDateWithTimeString:(NSString*) timeString{
    _dateFormatter = [self getDateFormatter];
    _dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";   //默认时间格式样式
    //    _dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en"];
    NSDate *date = [_dateFormatter dateFromString:timeString];
    //    NSDate *dateEn = [NSDate dateWithTimeInterval:3600*8 sinceDate:date];
    return date;
}

- (NSString *)getDateStringWithFormat:(NSString *)dateFormat;{
    _dateFormatter = [self getDateFormatter];
    _dateFormatter.dateFormat = dateFormat;
    NSString * dateStr = [_dateFormatter stringFromDate:self];
    return dateStr;
}


+ (NSDate *)getDateWithTimeString:(NSString*)timeString Format:(NSString *)dateFormat{
    _dateFormatter = [self getDateFormatter];
    _dateFormatter.dateFormat = dateFormat;
    _dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en"];
    NSDate *date = [_dateFormatter dateFromString:timeString];
    NSDate *dateEn = [NSDate dateWithTimeInterval:3600*8 sinceDate:date];
    return dateEn;
}


- (NSString *)getFormatYearMonthDay{
    NSString *string = [NSString stringWithFormat:@"%lu%02lu%02lu",(unsigned long)[self getYear],(unsigned long)[self getMonth],(unsigned long)[self getDay]];
    return string;
}

- (NSString *)getFormatYear_Month{
    NSString *string = [NSString stringWithFormat:@"%lu-%02lu",(unsigned long)[self getYear],(unsigned long)[self getMonth]];
    return string;
}

- (NSString *)getFormatMonthDay{
    NSString *string = [NSString stringWithFormat:@"%lu月%lu日",(unsigned long)[self getMonth],(unsigned long)[self getDay]];
    return string;
}

- (NSInteger)getWeekOfYear{
    NSInteger i;
    NSInteger year = [self getYear];
    NSDate *date = [self endOfWeek];
    for (i = 1; [[date dateAfterDay: -7 * i] getYear] == year;i++)
    {
    }
    return i;
}

- (NSDate *)dateAfterDay:(NSInteger)day{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    [componentsToAdd setDay:day];
    NSDate *dateAfterDay = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    return dateAfterDay;
}

- (NSDate *)dateafterMonth:(NSInteger)month{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    [componentsToAdd setMonth:month];
    NSDate *dateAfterMonth = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    return dateAfterMonth;
}

- (NSUInteger)getDay{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitDay) fromDate:self];
    return [dayComponents day];
}

- (NSUInteger)getMonth{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMonth) fromDate:self];
    return [dayComponents month];
}

- (NSInteger)getYear{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitYear) fromDate:self];
    return [dayComponents year];
}

- (NSInteger)getHour {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unitFlags fromDate:self];
    NSInteger hour = [components hour];
    return hour;
}

- (NSInteger)getMinute {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unitFlags fromDate:self];
    NSInteger minute = [components minute];
    return minute;
}

- (NSInteger)getHour:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    NSInteger hour = [components hour];
    return hour;
}

- (NSInteger)getMinute:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    NSInteger minute = [components minute];
    return minute;
}

- (NSUInteger)weekday {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitWeekday) fromDate:self];
    return [weekdayComponents weekday];
}

- (NSInteger)getNumberOfDaysInMonth{
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
                                   inUnit: NSCalendarUnitMonth
                                  forDate:self];
    return range.length;
}


#pragma mark 未使用-------------------------------------------------------
+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:format];
    NSDate *date = [inputFormatter dateFromString:string];
    return date;
}

+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format {
    return [date stringWithFormat:format];
}

+ (NSString *)stringFromDate:(NSDate *)date {
    return [date string];
}

- (NSString *)stringWithFormat:(NSString *)format {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:format];
    NSString *timestamp_str = [outputFormatter stringFromDate:self];
    return timestamp_str;
}

- (NSString *)string{
    return [self stringWithFormat:[NSDate dbFormatString]];
}

- (NSString *)stringWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateStyle:dateStyle];
    [outputFormatter setTimeStyle:timeStyle];
    NSString *outputString = [outputFormatter stringFromDate:self];
    return outputString;
}
//  返回周日的的开始时间
- (NSDate *)beginningOfWeek{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *beginningOfWeek = nil;
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth startDate:&beginningOfWeek
                           interval:NULL forDate:self];
    if (ok) {
        return beginningOfWeek;
    }
    NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    [componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];
    beginningOfWeek = nil;
    beginningOfWeek = [calendar dateByAddingComponents:componentsToSubtract toDate:self options:0];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay)
                                               fromDate:beginningOfWeek];
    return [calendar dateFromComponents:components];
}

//  返回当前天的年月日.
- (NSDate *)beginningOfDay{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay)
                                               fromDate:self];
    return [calendar dateFromComponents:components];
}

//  返回该月的第一天
- (NSDate *)beginningOfMonth{
    return [self dateAfterDay:(int)-[self getDay] + 1];
}

//  该月的最后一天
- (NSDate *)endOfMonth{
    return [[[self beginningOfMonth] dateafterMonth:1] dateAfterDay:-1];
}

//  返回当前周的周末
- (NSDate *)endOfWeek {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // Get the weekday component of the current date
    NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    // to get the end of week for a particular date, add (7 - weekday) days
    [componentsToAdd setDay:(7 - [weekdayComponents weekday])];
    NSDate *endOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    return endOfWeek;
}

+ (NSString *)dateFormatString {
    return @"yyyy-MM-dd";
}

+ (NSString *)timeFormatString {
    return @"HH:mm:ss";
}

+ (NSString *)timestampFormatString {
    return @"yyyy-MM-dd HH:mm:ss";
}

// preserving for compatibility
+ (NSString *)dbFormatString {
    return [NSDate timestampFormatString];
}

/** 获取当前的星期几 */
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate{
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return [weekdays objectAtIndex:theComponents.weekday];
}

-(NSString *) fullDescription{
    //1.获取需要比较的时间和当前时间的间隔
    NSCalendar *cl = [NSCalendar currentCalendar];
    if ([cl isDateInToday:self]) {
        //当天
        NSTimeInterval delta = (NSInteger)[self timeIntervalSinceDate:self];
        if (delta < 60) {
            return @"刚刚";
        } else if (delta < 60 *60) {
            //在一个小时内
            return [NSString stringWithFormat:@"%f分钟前",delta/60];
        } else {
            return [NSString stringWithFormat:@"%f小时前",delta/3600];
        }
    } else {
        //实例化 日期格式化对象
        NSString *comman = @"HH:mm";
        if ([cl isDateInYesterday:self]) {
            //昨天
            _dateFormatter.dateFormat = [NSString stringWithFormat:@"昨天%@",comman];
        } else {
            //非昨天
            //把系统当前日期和传过来的日期对象比较 两个日起对象之间相隔多少年
            NSDateComponents *result = [cl components:NSCalendarUnitYear fromDate:self toDate: [NSDate date]  options: 0];
            if (result.year == 0) {
                //当年
                _dateFormatter.dateFormat =[NSString stringWithFormat:@"MM-dd%@",comman];
            } else {
                //非当年
                _dateFormatter.dateFormat = [NSString stringWithFormat:@"yyyy-MM-dd%@",comman];
            }
        }
        NSString  *str = [_dateFormatter stringFromDate:self];
        return str;
    }
}

+ (NSDate *)getDataFromeTimeStr:(NSString *)timeStr formatterStr:(NSString *)formatterStr{
    
    NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en"]];
    [inputFormatter setDateFormat:formatterStr];
    NSDate *inputDate = [inputFormatter dateFromString:timeStr];
    
    return inputDate;
}



@end

